const weekNameMap = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
Component({
    properties: {
    mydate: String,  // 选中的天  Date String 格式
    mylist: Array,
  },
  data: {
    year: null,
    month: null,
    day: null,
    doctorId:null,
    firstday:null,
    list: [],
    colorkind:{},
  },
  lifetimes: {/*生命周期函数*/
    attached: function () {
      const now = new Date();
      this.setData({
        year: now.getFullYear(),
        month: now.getMonth() + 1,
        day: now.getDate(),
      }, this.calcDayList)
    },
  },
  observers: {/*属性监听*/
    'mydate': function (mydate) {
      let date = new Date(mydate);
      this.setData({
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        day: date.getDate(),
      }, this.calcDayList);
    },
    'mylist': function (mylist) {
      this.setData({ list: mylist }, this.calcDayList);
    },
  },
  methods: {// 这里是一个自定义方法
    call:function(e){
      var bookdate = this.data.year+"-"+this.data.month+"-"+e;
      console.log(bookdate);
       this.setData({
         doctorId: getApp().globalData.doctorId,
       })
       wx.request({
         url:  getApp().globalData.url+'doctorOrderTimeView', // 在PatientOrderController类中
         method: 'GET',
         data: {
           unitphone:getApp().globalData.unitphone,
           bookdate : bookdate,
           doctorId: getApp().globalData.doctorId,
         },
         header: {
           'content-type': 'application/x-www-form-urlencoded' // 默认值
         },
         success:(res)=>{
          var a=e;
          var k=this.data.firstday;
          console.log(a)
          console.log(this.data)
          if(a==null)
          {
            a=0;
          }
          console.log(a)
          var indexx = a+k-2;
           if(indexx<0)
           {
             indexx=0;
           }
          if(res.data.success=="True")
          {
           
            this.setData({
              [`dayList[${indexx}].colorkind`]:"true"
            })
            console.log(indexx)
          }
          else {
            this.setData({
              [`dayList[${indexx}].colorkind`]:"false"
            })
          }

       }
       ,fail:(res)=>
         {
           
         }
       })
       
    
     },
    // 获取当月共多少天
    getThisMonthDays: function (year, month) {
      return new Date(year, month, 0).getDate()
    },
    // 获取当月第一天星期几   0 星期日
    getFirstDayOfWeek: function (year, month) {
      return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    
    calcDayList: function () {
      const { year, month, list, } = this.data;
      let totalDay = this.getThisMonthDays(year, month);
      let firstDay = this.getFirstDayOfWeek(year, month);
      let dayList = [];
      console.log(firstDay)
      this.setData({
        firstday:firstDay
      })
      console.log(this.data.firstday)
    //  console.log(ubiojhsfdbhhbhb)
      for (let i = 0; i < (totalDay); i++) 
      {
          this.call(i);
          dayList.push({
          day: i + 1,
          weekName: weekNameMap[(firstDay + i) % 7],
        })
      }
      // 向前添加
      for (let i = 0; i < (firstDay || 7) - 1; i++) {
        dayList.unshift({ day: 0, });  
      }
      // 向后添加
      for (let i = 0; i < 42 - totalDay - ((firstDay || 7) - 1); i++) {
        dayList.push({ day: 0, });
      }
      // 当天
      let now = new Date();
      if (now.getFullYear() === year && now.getMonth() + 1 === month) {
        (dayList.find(d => d.day === now.getDate()) || {}).isToday = true;
        
      } 
     
      // 业务数据处理
      list.forEach(item => {
        let find = dayList.find(d => d.day === item.day);
        if (find) {
          Object.assign(find, item);
        }
      })    
      
      this.setData({ dayList, });
      for(var i=0;i<dayList.length;i++)
      {
        console.log(dayList[i])
      }
    },
  
    // 点击上个月
    handlePrevMonthClick: function () {
      let year = this.data.year;
      let month = this.data.month;
      if (this.data.month === 1) {
        year = this.data.year - 1;
        month = 12;
      } else {
        month = this.data.month - 1;
      }
      this.handleDateChange({ year, month });
    },
    // 点击下个月
    handleNextMonthClick: function () {
      let year = this.data.year;
      let month = this.data.month;
      if (this.data.month === 12) {
        year = this.data.year + 1;
        month = 1;
      } else {
        month = this.data.month + 1;
      }
      this.handleDateChange({ year, month });
    },
    // 点击 天
    handleDayClick: function (e) {     
      const { day } = e.currentTarget.dataset;
      this.handleDateChange({ day });
      var chooseday =e.currentTarget.dataset.day;
      console.log(chooseday);
      var m=this.data.firstday;
      m+=chooseday-2;
      console.log(m)
      var k=this.data.dayList[m].colorkind;
      console.log(k);
      if(this.data.dayList[m].colorkind=='true')
      {   
       var bookdate = this.data.year+"-"+this.data.month+"-"+chooseday;
       console.log(bookdate)
        getApp().globalData.bookdate=bookdate
        wx.navigateTo({
          url:'../introduce/introduce',
        })
        
      }
      else /*if(this.data.dayList[m].colorkind=='false')*/{
        wx.showToast({
          title: '该日已约满或您已预约该日',
          icon:'none'
        })
       
      }
    },
  
    // 处理日期变化
    handleDateChange: function (time) {
      const { year, month, day } = this.data;
      let date = { year, month, day, ...time };
      if (!this.data.mydate) {
        this.setData({ ...date });
        if (time.year || time.month) 
          this.calcDayList();
      }
      //this.call();
      this.triggerEvent('mydatechange', { date: new Date(`${date.year}/${date.month}/${date.day}`).toString() })
    }
  }
})